package com.wzy.mall.service.client.impl;

import com.wzy.mall.mapper.clientMapper.UserMapper;
import com.wzy.mall.model.bo.clientBO.LoginBO;
import com.wzy.mall.model.bo.clientBO.SignupBO;
import com.wzy.mall.model.bo.clientBO.UserPwdBO;
import com.wzy.mall.model.bo.clientBO.UserUpdateDataBO;
import com.wzy.mall.model.vo.clientVO.LoginVO;
import com.wzy.mall.model.vo.clientVO.UserDataVO;
import com.wzy.mall.service.client.UserService;
import com.wzy.mall.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl implements UserService {

    public LoginVO login(LoginBO loginBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        LoginVO loginVO  = null;
        try {
            loginVO = mapper.selectUserByEmailAndPassword(loginBO);
            //System.out.println(loginVO);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return loginVO;
    }

    public LoginVO signup(SignupBO signupBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        int result = 0;
        LoginVO loginVO = null;
        try {
            result = mapper.insertUser(signupBO);
            if(result != 0) {
                loginVO = new LoginVO();
                loginVO.setCode(0);
                loginVO.setName(signupBO.getNickname());
                loginVO.setToken(signupBO.getNickname());
            }

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return loginVO;
    }

    public UserDataVO getUserData(String token) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        UserDataVO userDataVO = null;
        try {
            userDataVO = mapper.selectUserByNickname(token);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return userDataVO;
    }

    public int updatePwdByID(UserPwdBO userPwdBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        // 首先确认旧密码是否匹配
        int result = 0;
        try {
            result = mapper.selectPwdByID(userPwdBO);
            if(result != 0) {
                // 密码匹配
                // 进行更改操作
                result = mapper.updatePwdByID(userPwdBO);

                sqlSession.commit();
            } else {
                result = 404;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return result;
    }

    public int updateUserData(UserUpdateDataBO userUpdateDataBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        int result = 0;
        try {
            result = mapper.updateUserData(userUpdateDataBO);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return result;
    }
}
